﻿using LD.Controls.ControlHelper;
using ScottPlot;
using System;
using System.Collections.Generic;
using System.IO;

namespace LD
{
    public partial class FrmCCD1 : FrmBase
    {
        private string filePath = Path.Combine(Appbus.BasePath, "CCD");
        public FrmCCD1()
        {
            InitializeComponent();
          
            Init();

            FormScaler.ScaleForm(this, true);//自动缩放

        }
        private void Instance_ReceiveDataEvent(string name, TB_Screw screwData, List<ScrewDataModel> data)
        {
            if (!this.IsDisposed)
            {
                this.Invoke(new Action(() =>
                {
                    switch (name)
                    {
                        case "B轨_电批":
                            ParseData("B", screwData, data);
                            break;
                        case "A轨_电批":
                            ParseData("A", screwData, data);
                            break;
                        default:
                            break;
                    }
                }));
            }
        }
        private void ParseData(string L_R, TB_Screw screwData, List<ScrewDataModel> data)
        {
            Plot mPlot = new Plot();

            if (L_R == "B")
            {
                //mPlot = formsPlot1.Plot;
                //mPlot.Clear();
                //formsPlot1.Refresh();
            }
            else
            {
                mPlot.Clear();
            }
            if (data == null)
                return;

            double[] x = new double[data.Count];
            double[] y = new double[data.Count];
            for (int i = 0; i < data.Count; i++)
            {
                x[i] = data[i].角度;
                y[i] = data[i].扭矩;
            }
            if (x.Length > 0 && y.Length > 0)
            {
                if (L_R == "B")
                {
                    mPlot.AddScatterLines(x, y);

                    double.TryParse(PartNoHelper.Instance.CurrentPartNo["B轨_组装"].Parame[$"扭矩上限"], out var up);
                    double.TryParse(PartNoHelper.Instance.CurrentPartNo["B轨_组装"].Parame[$"扭矩下限"], out var down);
                    mPlot.AddHorizontalLine(down);
                    mPlot.AddHorizontalLine(up);
                    mPlot.Title($"B轨_{screwData.Location}_曲线");

                    //formsPlot1.Refresh();
                }
                else
                {
                    mPlot.AddScatterLines(x, y);

                    double.TryParse(PartNoHelper.Instance.CurrentPartNo["A轨_组装"].Parame[$"扭矩上限"], out var up);
                    double.TryParse(PartNoHelper.Instance.CurrentPartNo["A轨_组装"].Parame[$"扭矩下限"], out var down);
                    mPlot.AddHorizontalLine(down);
                    mPlot.AddHorizontalLine(up);
                    mPlot.Title($"A轨_{screwData.Location}_曲线");

                }
            }

            var bitmap = mPlot.Render();
            bitmap.Save(screwData.FilePath+".jpg");
            //string _filePath = Path.Combine(screwData.ImgUrl, screwData.ImgName + ".jpg");
            //MesHelper.Instance.UploadImg(screwData.Barcode, screwData.ImgName + ".jpg", _filePath);
        }
        private void FrmCCD_Load(object sender, EventArgs e)
        {

        }
        private void Init()
        {
            //this.formsPlot1.Configuration.ScrollWheelZoom = false;
            //this.formsPlot1.Configuration.LockHorizontalAxis = false;
            //this.formsPlot1.Configuration.LockVerticalAxis = false;

            //var leftPlot = formsPlot1.Plot;

            //leftPlot.Title("扭力曲线显示区域");
            //leftPlot.XLabel("角度");
            //leftPlot.YLabel("扭矩");

            //formsPlot1.Refresh();

            //TODO  根据电批数量来定制数据
            //ScrewHelper.Instance.ReceiveDataEvent += Instance_ReceiveDataEvent;
        }
        public void ChangePartNo(string ccdName)
        {

        }
        private void FrmCCD_Shown(object sender, EventArgs e)
        {
            var _ccdFilePath = Path.Combine(filePath, PartNoHelper.Instance.CurrentPartNo.CCDName);
            SciHelper.Instance.Init();

            SciHelper.Instance.Pic_LeftUp = this.pic_leftUp.Handle;
            SciHelper.Instance.Pic_LeftDown = this.pic_leftdown.Handle;


            SciHelper.Instance.Open(_ccdFilePath);


            //var mPlot2 = formsPlot1.Plot;
            //double.TryParse(PartNoHelper.Instance.CurrentPartNo["B轨_组装"].Parame[$"扭矩上限"], out var up2);
            //double.TryParse(PartNoHelper.Instance.CurrentPartNo["B轨_组装"].Parame[$"扭矩下限"], out var down2);
            //mPlot2.AddHorizontalLine(up2);
            //mPlot2.AddHorizontalLine(down2);
            //formsPlot1.Refresh();
        }
    }
}
